import http

from flask import Flask, url_for,jsonify
from flask_restful import Api, Resource,reqparse
from flask_httpauth import HTTPAuth
from itsdangerous import Serializer
from config import JwtConfig
from jwt_demo import jwt
from flask_jwt_extended import create_access_token,get_jwt_identity,jwt_required
from flasgger import Swagger

from reqparse_demo import Login
from marshal_demo import UserResource,UserResource2,UserResourceRename,UserResourceFiledsList,UserResourceNested


app = Flask(__name__)
# 导入flask实例app已构造一个api实例
api = Api(app)
SECRET_KEY = "dasfdsadfsda"
# auth = HTTPAuth(scheme="TOKEN")
app.config.from_object(JwtConfig)
jwt.init_app(app)

#swagger
app.config['SWAGGER']={
    'title':"在线API"
}

swagger = Swagger(app)


class IndexView(Resource):
    def get(self):
        return {"info": "hello"}


api.add_resource(IndexView, "/index", endpoint="hello")
api.add_resource(Login, "/login", endpoint="login")
api.add_resource(UserResource, "/show1/<string:id>", endpoint="show1")
api.add_resource(UserResource2, "/show2/<string:id>", endpoint="show2")
api.add_resource(UserResourceRename, "/show3/<string:id>", endpoint="show3")
api.add_resource(UserResourceFiledsList, "/show4/<string:id>", endpoint="show4")
api.add_resource(UserResourceNested, "/show5", endpoint="show5")



@app.route("/test")
def test():

    return url_for("hello")


@jwt.expired_token_loader
def expire_token(jwt_header,jwt_payload):
    print(jwt_header)
    print(jwt_payload)
    return jsonify(code=401,message="token过期")
@app.route("/veify",methods=["GET", "POST"])
@jwt_required()
def verify_token():
    t = get_jwt_identity()
    print(123232)
    return t
@app.route('/')
def hello_world():  # put application's code here
    token = create_access_token(identity="dasfdsaf")
    return token

@app.route('/add')
def add():
    """
    This is the language awesomeness API
    Call this api passing a language name and get back its features
    ---
    tags:
      - Awesomeness Language API
    parameters:
      - name: language
        in: path
        type: string
        required: true
        description: The language name
      - name: size
        in: query
        type: integer
        description: size of awesomeness
    responses:
      500:
        description: Error The language is not awesome!
      200:
        description: A language with its awesomeness
        schema:
          id: awesome
          properties:
            language:
              type: string
              description: The language name
              default: Lua
            features:
              type: array
              description: The awesomeness list
              items:
                type: string
              default: ["perfect", "simple", "lovely"]
    """
    return jsonify(result="100"),200

if __name__ == '__main__':
    app.run()
